<?
	include_once('header.php');

	include_once('sidebar.php');

	$offset = $_GET['offset'];
	$limit = $_GET['limit'];
	$action = $_GET['action'];
	$id = $_GET['id'];
	if(!$offset) { $offset = "0"; }
	if(!$limit) { $limit = "20"; }
?>

		
		<div id="main">
			
			<h2>Events</h2>
<?
	if(!drizzo_user_is_allowed('events-write') || drizzo_global_options('module_events') != 1) {
		echo "<p>This module is not available.</p><br class=\"clear\" />";
		include_once('footer.php');
		exit();
	}
?>
			<br class="clear" />
<?
	if($_POST && $action != 'delete') {
		if(drizzo_event_save()) {
			echo "<p class=\"save\">" . drizzo_db_unwrap($_POST['what']) . " has been " . str_replace("eed","ed",$action . "ed") . ".</p>\n";
		} else {
			echo "<p class=\"alert\">" . drizzo_db_unwrap($_POST['what']) . " could not be " . str_replace("eed","ed",$action . "ed") . ".</p>\n";
		}
	} elseif($_POST && $action == 'delete') {
		if(drizzo_event_delete()) {
			echo "<p class=\"trash\">" . drizzo_db_unwrap($_POST['what']) . " has been deleted.</p>\n";
		} else {
			echo "<p class=\"trash\">" . drizzo_db_unwrap($_POST['what']) . " could not be deleted.</p>\n";
		}
	}
	if(!$action || ($action && $_POST)) {
		echo drizzo_events_table(null,null,null,null,'publish,draft');
	} elseif(($action == 'add' || $action == 'edit') && !$_POST) {
		$the_event = drizzo_get_event($id);
?>
			<form action="" method="post" id="events_form" onsubmit="return checkDateRange()">
				<div id="editor">
					<label for="what">Title</label> <input type="text" name="what" value="<? echo $the_event['what']; ?>" /><br class="clear" />
					<label for="where">Location Name</label> <input type="text" name="where" value="<? echo $the_event['where']; ?>" /><br class="clear" />
					<label for="address">Location Address</label> <input type="text" name="address" value="<? echo $the_event['address']; ?>" /><br class="clear" />
					<label for="who">Who</label> <input type="text" name="who" value="<? echo $the_event['who']; ?>" /><br class="clear" />
					<label for="notes">Notes</label> <textarea name="notes"><? echo $the_event['notes']; ?></textarea><br class="clear" />
					<label for="tags">Tags<br /><small>(Separate by commas)</small></label><textarea name="tags" type="text" id="tags_input"></textarea><br />
					<label for="recent_tags">Recently Used Tags</label><p id="recent_tags"><? echo drizzo_recent_tags(); ?></p>
				</div>
				<div id="options">
					<h3>Start</h3><? echo drizzo_date_dropdowns('start_'); ?><br />
					<h3>End</h3><? echo drizzo_date_dropdowns('end_'); ?><br />
					<label for="all_day" class="inline">Is this an all day event?</label><input type="checkbox" onclick="allDayEvent()" id="all_day" class="inline checkbox" />
					<? echo drizzo_post_authors_dropdown(); ?>
					<h3>Permalink</h3> <input type="text" name="permalink_name" value="<? echo $the_event['permalink_name']; ?>" id="permalink_name" />
					<br class="clear" />
				</div>
				<input type="hidden" name="ID" value="<? echo $the_event['ID']; ?>" />
				<input type="hidden" name="added_by" value="<? echo $the_event['added_by']; ?>" />
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="draft" value="Save as Draft" class="submit" /><input type="submit" name="publish" value="Publish" class="submit" /></p>
			</form>
<?
	} elseif($action == 'delete' && !$_POST) {
		$the_event = drizzo_get_event($id);
?>
			<form action="" method="post">
				<p>Are you sure you want to delete <strong><? echo $the_event['what']; ?></strong>?</p>
				<input type="hidden" name="what" value="<? echo $the_event['what']; ?>" />
				<p class="right"><input type="button" onclick="history.go(-1)" value="Cancel" class="submit" /><input type="submit" name="delete" value="Delete" class="submit" /></p>
			</form>
<?
	}
?>
			<br class="clear" />
			
		</div>
<?
	include_once('footer.php');
?>
